﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Macds.Core.XML.Excel
{
    public class ExcelWorksheet : ExcelBase
    {
        public ExcelColumnCollection Columns { get; set; }
        public ExcelRowsCollection Rows { get; set; }
        public string Name { get; set; }

        public ExcelWorksheet()
        {
            this.Name = "Sheet1";
            this.Columns = new ExcelColumnCollection();
            this.Rows = new ExcelRowsCollection();
        }

        public override string ToString()
        {
            StringBuilder content = new StringBuilder();
            content.Append("<ss:Worksheet ss:Name=\"" + this.Name + "\">\r\n");
            content.Append("<ss:Table ss:ExpandedColumnCount=\"");
            
            
            if(this.Columns.Count < 1)
            {
                if(this.Rows.Count > 0)
                {
                    for (int i = 0; i < this.Rows[0].Cells.Count; i++)
                    {
                        this.Columns.Add(new ExcelColumn());
                    }
                }
            }

            content.Append(Convert.ToString(this.Columns.Count + 1));
            content.Append("\" ss:ExpandedRowCount=\"");
            content.Append(Convert.ToString(this.Rows.Count + 1));
            content.Append("\" x:FullColumns=\"1\" x:FullRows=\"1\">\r\n");

            foreach (ExcelColumn c in Columns)
            {
                content.Append(c.ToString());
            }

            foreach (ExcelRow r in Rows)
            {
                content.Append(r.ToString());
            }

            content.Append("</ss:Table>\r\n");
            content.Append("</ss:Worksheet>\r\n");
            return content.ToString();
        }
    }
}
